package inventoryservicelogic

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"property_service/dao/model"
	"property_service/dao/query"
	"property_service/internal/svc"
	"property_service/property_service"
	"property_service/utils/BeanUtils"
)

type AssetInventoryPageLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAssetInventoryPageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AssetInventoryPageLogic {
	return &AssetInventoryPageLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// AssetInventoryPage 分页查询资产库存表项
func (l *AssetInventoryPageLogic) AssetInventoryPage(in *property_service.AssetInventoryPageReq) (*property_service.AssetInventoryPageRes, error) {
	// 定义一个资产库存表模型对象切片用于存储查询结果
	var data []*model.AssetInventory
	// 获取资产库存表模型的查询构造器
	m := query.Use(l.svcCtx.Gorm).AssetInventory
	// 构建查询条件：根据租户ID查询
	do := m.WithContext(l.ctx).Where(m.TenantID.Eq(in.Where.TenantID))
	// 动态构建查询条件
	if in.Where.ID > 0 {
		do = do.Where(m.ID.Eq(in.Where.ID)) // ID
	} else {
		// 处理字符类型字段，进行模糊查询
		if in.Where.AssetCode != "" {
			do = do.Where(m.AssetCode.Like("%" + in.Where.AssetCode + "%")) // 资产编号
		}
		if in.Where.AssetName != "" {
			do = do.Where(m.AssetName.Like("%" + in.Where.AssetName + "%")) // 资产名称
		}
		if in.Where.AssetCategory != "" {
			do = do.Where(m.AssetCategory.Like("%" + in.Where.AssetCategory + "%")) // 资产类别
		}
		if in.Where.SpecModel != "" {
			do = do.Where(m.SpecModel.Like("%" + in.Where.SpecModel + "%")) // 规格型号
		}
		if in.Where.StorageLocation != "" {
			do = do.Where(m.StorageLocation.Like("%" + in.Where.StorageLocation + "%")) // 存放地点
		}
		if in.Where.Supplier != "" {
			do = do.Where(m.Supplier.Like("%" + in.Where.Supplier + "%")) // 供应商
		}
		if in.Where.UsingDept != "" {
			do = do.Where(m.UsingDept.Like("%" + in.Where.UsingDept + "%")) // 使用科室
		}
		if in.Where.StatusName != "" {
			do = do.Where(m.StatusName.Like("%" + in.Where.StatusName + "%")) // 采购状态
		}
		if in.Where.InventoryStatus != "" {
			do = do.Where(m.InventoryStatus.Like("%" + in.Where.InventoryStatus + "%")) // 状态
		}
		if in.Where.CountryOfOrigin != "" {
			do = do.Where(m.CountryOfOrigin.Like("%" + in.Where.CountryOfOrigin + "%")) // 状态
		}
		if in.Where.Brand != "" {
			do = do.Where(m.Brand.Like("%" + in.Where.Brand + "%")) // 状态
		}
		if in.Where.ManufacturersName != "" {
			do = do.Where(m.ManufacturersName.Like("%" + in.Where.ManufacturersName + "%")) // 状态
		}

		// 处理时间类型字段，进行精确查询
		if in.Where.PurchaseDate != "" {
			do = do.Where(m.PurchaseDate.Eq(BeanUtils.GetDateFormat(in.Where.PurchaseDate))) // 购置日期
		}

		// 处理数字类型字段，进行精确查询
		if in.Where.AssetCategoryID > 0 {
			do = do.Where(m.AssetCategoryID.Eq(in.Where.AssetCategoryID)) // 资产编号id
		}
		if in.Where.Quantity > 0 {
			do = do.Where(m.Quantity.Eq(in.Where.Quantity)) // 库存数量
		}
		if in.Where.UseNum > 0 {
			do = do.Where(m.UseNum.Eq(in.Where.UseNum)) // 已使用数量
		}
		if in.Where.SafetyStock > 0 {
			do = do.Where(m.SafetyStock.Eq(in.Where.SafetyStock)) // 安全库存
		}
		if in.Where.SupplierID > 0 {
			do = do.Where(m.SupplierID.Eq(in.Where.SupplierID)) // 供应商id
		}
		if in.Where.UsingDeptID > 0 {
			do = do.Where(m.UsingDeptID.Eq(in.Where.UsingDeptID)) // 使用科室id
		}
		if in.Where.StatusID > 0 {
			do = do.Where(m.StatusID.Eq(in.Where.StatusID)) // 状态ID
		}
		if in.Where.AssetID > 0 {
			do = do.Where(m.AssetID.Eq(in.Where.AssetID)) // 资产ID
		}
		if in.Where.StockID > 0 {
			do = do.Where(m.StockID.Eq(in.Where.StockID)) // 仓库ID
		}
		if in.Where.CountryOfOriginID > 0 {
			do = do.Where(m.CountryOfOriginID.Eq(in.Where.CountryOfOriginID)) // 仓库ID
		}
		if in.Where.BrandID > 0 {
			do = do.Where(m.BrandID.Eq(in.Where.BrandID)) // 仓库ID
		}
		if in.Where.ManufacturersID > 0 {
			do = do.Where(m.ManufacturersID.Eq(in.Where.ManufacturersID)) // 仓库ID
		}
	}

	// 定义一个变量用于存储总记录数
	var count int64

	// 判断是否需要分页
	if in.Page+in.Limit <= 1 {
		// 如果不需要分页，查询所有记录
		data, _ = do.Find()
		// 设置总记录数为查询结果的长度
		count = int64(len(data))
	} else {
		// 如果需要分页，查询指定页的数据和总记录数
		data, count, _ = do.FindByPage(int((in.Page-1)*in.Limit), int(in.Limit))
	}

	// 将查询结果转换为响应对象
	list, _ := BeanUtils.CopyProperties(data, property_service.AssetInventory{})

	// 返回查询结果，包括总记录数和数据列表
	return &property_service.AssetInventoryPageRes{
		Count: count,
		List:  list.([]*property_service.AssetInventory),
	}, nil
}
